<template>
  <view class="address">
    <view class="addaddress" v-if="!addressInfo.userName">
      <button type="primary" size="mini" @click="chooseAddress">请添加收货地址+</button>
    </view>
    <view class="user_address" v-else>
      <view class="row1">
        <text>收货人:{{addressInfo.userName}}</text>
        <view class="">
          <text>电话:{{addressInfo.telNumber}}  </text>
          <uni-icons type="forward" size="16" @click="chooseAddress"></uni-icons>
        </view>
      </view>
      <view class="row2">
        <text>收货地址:</text>
        <text>{{addressInfo.provinceName}}{{addressInfo.cityName}}{{addressInfo.countyName}}{{addressInfo.detailInfo}}</text>
      </view>
    </view>
    <image src="../../static/cart_border@2x.png" mode="widthFix"></image>
  </view>
</template>

<script>
  import {mapMutations,mapState} from "vuex"
  export default {
    name: "Myaddress",
    data() {
      return {
      addressInfo:{}
      };
    },
    methods:{
      ...mapMutations('cartStore',['addAddress']),
      async chooseAddress(){
        if(uni.getStorageSync('token')=='') return uni.$showMsg('您还未登录，无法选择收货地址')
        let [err,res]=await uni.chooseAddress()
        if(err===null && res.errMsg==="chooseAddress:ok"){
          this.addressInfo=res
          this.addAddress(res)
        }
      }
    },
    computed:{
      ...mapState('cartStore',['address'])
    },
    watch:{
      address:{
        immediate:true,
        handler(newval){
        this.addressInfo=newval
      }
      }
    },
    onShow() {
      this.addressInfo=uni.getStorageSync('address')
    },



  }
</script>

<style lang="scss">
  .address {
    font-size: 12px;
    color: #333;
    display: flex;
    flex-direction: column;

    .addaddress {
      height: 80px;
      display: flex;
      justify-content: center;
      align-items: center;
    }
    .user_address{
      height: 80px;
      display: flex;
      flex-direction: column;
      justify-content: space-around;
      padding: 5px 5px;
      .row1{
        display: flex;
        justify-content: space-between;
      }
      .row2{
        display: flex;
        & text:nth-child(1){
          display: flex;
          align-items: center;
          width: 15%;
        }
        & text:nth-child(2){
          width: 85%;
         word-break:break-all;
        }
      }
    }

    image {
      width: 100%;
    }
  }
</style>
